﻿/// <reference path="jquery-1.2.6.min.js" />
/// <reference path="formValidatorRegex.js" />

function EvalContextMenu(t) {
    if (t.onclick == null) {
        return;
    }
    t.onclick();
    eval($(t).attr("hrefbak"));
}

function RefreshPage() {
    var href = window.location.href;
    href = href.replace(/&?random=[\d\.]+/gmi, "");
    if (href.indexOf("?") == -1)
    { href = href + "?" }
    else
    { href = href + "&" }

    href = href + "random=" + Math.random();
    window.location.href = href;
}

function SetCIfrHeight(tag) {
    if (tag == null) {
        tag = "ifrEdit";
    }
    try { document.getElementById(tag).height = document.getElementById(tag).windows.document.body.scrollHeight; } catch (e) { }


}

function SetIfrHeight(tag) {
    if (tag == null) {
        tag = "ifrEdit";
    }
    try { parent.document.getElementById(tag).height = document.body.scrollHeight; } catch (e) { }
    try { parent.document.getElementById("ifrMain").height = document.body.scrollHeight; } catch (e) { }

}



function InitialCheckBox(treeName) {
    ///初始化checkbox框不能选择
    $(":checkbox").each(
            function() {
                if (this.id.indexOf(treeName) == 0) {
                    //$(this).attr("disabled","disabled");
                    $(this).click(function() {
                        return false;
                    });
                }
            });
}
function ConfirmDelete(msg) {
    if (msg == null) {
        msg = "您确认要删除这条记录吗?";
    }
    return confirm(msg);
}
function ConfirmCancel(msg) {
    if (msg == null) {
        msg = "您所做的修改将丢失\r\n你确实要执行吗?";
    }
    return confirm(msg);
}
function ConfirmReset(msg) {
    if (msg == null) {
        msg = "表单里面的数据将还原为默认值\r\n你确实要重置表单里面的数据吗?";
    }
    return confirm(msg);
}
function ConfirmReturn(msg) {
    if (msg == null) {
        msg = "您所做的修改将丢失\r\n您确实要离开当前页面吗?";
    }
    return confirm(msg);
}
function GoPageUrl(pageUrl) {
    //pageUrl=escape(pageUrl);
    window.location.href = pageUrl;
}
/*添加指定id的表格鼠标滑过事件begin*/
function addTrHover(tableID) {
    var i = 0;

    var a = $("#" + tableID + "").find("tr").each(function() {
        i++;
        var aa = $(this);
        if (i % 2 == 0) {
            aa.addClass("tb_tr2");
        } else {
            aa.addClass("tb_tr2_alter");
        }

        aa.mouseover(mouseTrOver);
        aa.mouseout(mouseTrOut);
    });

}
function mouseTrOut() {
    var qtt = $(this);
    qtt.attr("class", qtt.attr("tclass"));
}
function mouseTrOver() {
    var qtt = $(this);
    if (qtt.attr("tclass") == null) {
        qtt.attr("tclass", qtt.attr("class"));
    }
    qtt.attr("class", "tb_tr2_hover");

}
/*添加指定id的表格鼠标滑过事件end*/
var timeHander;
function ShowMsg(tagID, msgID, minWidth, msg, speed) {
    ///<summary>显示指定控件的指定消息,可以指定控件的customMsg属性</summary>
    ///<param name="msg">显示消息</param>
    ///<param name="speed">显示速度</param>
    var tagQ = $("#" + tagID);
    if (tagQ.length == 0) {
        return;
    }
    var tagQIsVali = tagQ.attr("msgState");
    if (msgID == null) {
        msgID = "divMsg" + tagID;
    }
    var tagMsg = $("#" + msgID);
    if (tagMsg.length == 0) {
        return;
    }
    if (msg == null) {
        if (tagQIsVali == null) {
            var tagEntry = $("#" + tagID);
            var checkType = tagEntry.attr("checkType");
            var tagValue = tagEntry.val();
            var maxLength = tagEntry.attr("maxLength");
            var minLength = tagEntry.attr("minLength");
            var maxValue = tagEntry.attr("maxValue");
            var minValue = tagEntry.attr("minValue");
            var isEmptyEnable = tagEntry.attr("isEmptyEnable");
            var defaultValue = tagEntry.attr("defaultVal");
            var customRegex = tagEntry.attr("customRegex");
            var customMsg = tagEntry.attr("customMsg");
            if (checkType == null) {//兼容现有代码
                msg = tagMsg.html();
            }
            else {
                if (maxLength == null) { maxLength = 20 };
                if (minLength == null) { minLength = 0 };
                if (maxValue == null) { maxValue = 65535 };
                if (minValue == null) { minValue = 0 };

                var msgIsEmptyEnable = "";
                var msgContent = "";
                if (isEmptyEnable == "0")//不为空
                {
                    msgIsEmptyEnable = "<span style='color:red'>*</span>";
                }

                if (tagValue.length == 0 && defaultValue != null) {
                    tagEntry.val(defaultValue);
                }

                if (customMsg == null)//自定义消息
                {
                    if (checkType == "string") {
                        msgContent = "长度必须在" + minLength + "到" + maxLength + "之间";
                    }
                    else if (checkType == "datetime") {
                        msgContent = "时间必须在" + minValue + "到" + maxValue + "之间";
                    }

                    else {
                        msgContent = "值必须在" + minValue + "到" + maxValue + "之间";
                    }
                }
                else {
                    msgContent = customMsg;
                }
                //
                msg = msgIsEmptyEnable + msgContent;
            }

            tagMsg.attr("msgState", "pro"); //提示信息
            tagMsg.attr("proMsg", msg);
        }
        else {
            msg = tagMsg.attr(tagQIsVali + "Msg");
        }

        if (msg == null || $.trim(msg) == "") {
            return false;
        }

    }

    SetMsgPos(tagID, msgID, minWidth);
    var lastText = tagMsg.text();
    tagMsg.mouseover(setVisible);
    tagMsg.mouseout(setHidden);
    tagMsg.click(setHidden);
    tagMsg.dblclick(setVisible);


    if (lastText != msg) {
        tagMsg.hide();
    }
    tagMsg.html(msg);
    tagQ.blur(//失去焦点时候验证
            function() {
                checkValidate(tagID, msgID);
            });
    if (speed == 0) {
        tagMsg.show();
        tagQ.focus(
            function() { setVisible(msgID, tagID); }
            );

    }
    else if (speed == null) {
        tagMsg.show(1000);
    }
    else {
        tagMsg.show(speed);
    }

}
function SetMsgPos(tagID, msgID, minWidth) {
    var tagQ = $("#" + tagID);
    if (tagQ.length == 0) {
        return;
    }
    var tagMsg = $("#" + msgID);
    if (tagMsg.length == 0) {
        return;
    }
    var tagOffset = tagQ.offset();
    var tagTop = tagOffset.top;
    var tagLeft = tagOffset.left;
    tagLeft = tagLeft + tagQ.width() + 5;


    var tagParent = tagQ.parent();
    var tagParentLeft = tagParent.offset().left;
    var width = tagParent.width() + tagParentLeft - tagLeft;
    if (width < minWidth) {
        if (minWidth != null)
        { width = minWidth; }
    }

    var height = tagQ.height() * 1.2;
    tagMsg.css("top", tagTop + "px");
    tagMsg.css("left", tagLeft + "px");
    tagMsg.css("width", width + "px");
    tagMsg.css("height", height + "px");
    tagMsg.css("z-index", (99999 - tagTop));
}

function TimeOutArray(refFun, tag) {
    return (
        function() {
            refFun(tag);
        }
        );

}

function setVisible(msgID) {
    var qt;
    if (typeof (msgID) == 'object') {
        qt = $(this);
    }
    else {
        qt = $("#" + msgID);
    }
    var height = qt.get(0).scrollHeight;
    var lastHeight = qt.attr("lastHeight");
    if (lastHeight == null) {
        qt.attr("lastHeight", qt.css("height"));
    }

    var customMethodMsg = qt.attr("customMethodMsg");
    if (customMethodMsg == null || customMethodMsg == "") {
        qt.html(qt.attr("proMsg"));
    } else {
        qt.html(customMethodMsg);
    }


    qt.css("height", height);

    qt.css("border-color", "blue");
    var isError = qt.attr("isError");
    if (isError == null || isError == "false") {
        qt.css("color", "blue");
    } else {
        qt.css("color", "red");
    }

    qt.css("overflow", "visible");
    qt.css("background-color", "InfoBackground");
    if (qt.get(0).scrollHeight < qt.attr("lastHeight").replace("px", "")) {//新的高度小于原来的高度
        qt.css("height", qt.attr("lastHeight"));
    }


}

function setHidden(msgID) {
    var qt;
    if (typeof (msgID) == 'object') {
        qt = $(this);
    }
    else {
        qt = $("#" + msgID);
    }

    var height = qt.attr("lastHeight");
    if (height != null) {
        qt.css("height", height);

    }



    var isError = qt.attr("isError");
    if (isError == "true") {
        var customMethodMsg = qt.attr("customMethodMsg");
        if (customMethodMsg == null || customMethodMsg == "") {
            qt.html(qt.attr("proMsg"));
        } else {
            qt.html(customMethodMsg);
        }
        qt.css("border-color", "red");
        qt.css("color", "red");


    }
    else if (isError == "false") {
        qt.html("<span> 验证成功</span>");
        qt.css("border-color", "green");
        qt.css("color", "green");
    }
    else {
        qt.css("border-color", qt.css("background-color"));
        qt.css("color", "black");
    }

    qt.css("overflow", "hidden");
}

var validatorSucess = true;
function InitialMsg(minWidth) {


    //    $("form").submit(
    //        function()
    //        {
    //            return checkAll();
    //        }
    //        );
    var msgPrefix = "input";
    $(":" + msgPrefix).each(
        function() {
            var tagID = this.id;
            var msgID = getServerID(tagID);
            msgID = "divMsg" + msgID;
            if (!(tagID == null || tagID == "")) {
                ShowMsg(tagID, msgID, minWidth, null, 0);

            }
        }
        );
    $(window).resize(function() {
        $(":" + msgPrefix).each(
        function() {
            var tagID = this.id;
            var msgID = getServerID(tagID);
            msgID = "divMsg" + msgID;
            if (!(tagID == null || tagID == "")) {
                SetMsgPos(tagID, msgID, minWidth);

            }
        }
        );
    });

}
function checkAll(tag) {

    ///检查整个表单
    validatorSucess = true;
    var msgPrefix = "input";
    $(":" + msgPrefix).each(
        function() {
            var tagID = this.id;
            var msgID = getServerID(tagID);
            msgID = "divMsg" + msgID;

            if (!(tagID == null || tagID == "")) {
                checkValidate(tagID, msgID, true);

            }
        }
        );

    if (validatorSucess)
    { validatorSucess = checkAllAfter(tag); }
    
    if (validatorSucess) {

        return true;
    } else {
        alert("页面存在错误,请检查");
        return false;
    }
}
function checkAllAfter(tag) {
    
    var btn = $(tag);
    if (btn.length == 0) {
        return true;
    }
    var customMethod = btn.attr("customMethod");
    if (customMethod == null || customMethod == "") {
        return true;
    }
    var result = eval(customMethod + "()")
    if (result == "true") {
        return true;
    } else {
        return false;
    }

}

function checkValidate(tagID, msgID, isCheckAll) {
    
    var tagEntry = $("#" + tagID);
    var checkType = tagEntry.attr("checkType");
    var tagValue = tagEntry.val();
    var maxLength = tagEntry.attr("maxLength");
    var minLength = tagEntry.attr("minLength");
    var maxValue = tagEntry.attr("maxValue");
    var minValue = tagEntry.attr("minValue");
    var isEmptyEnable = tagEntry.attr("isEmptyEnable");
    var customRegex = tagEntry.attr("customRegex");
    var isRegexEnum = tagEntry.attr("isRegexEnum");
    var customMsg = tagEntry.attr("customRegex");
    var defaultValue = tagEntry.attr("defaultVal");
    var customMethod = tagEntry.attr("customMethod");
    var nameSpace = tagEntry.attr("nameSpace");
    var isValidate = true;
    var msgEntry = $("#" + msgID);
    if (checkType == null) {
        return;
    }
    if (maxLength == null) { maxLength = 20 };
    if (minLength == null) { minLength = 0 };
    if (maxValue == null) { maxValue = 65535 };
    if (minValue == null) { minValue = 0 };
    if (tagValue.length == 0) {
        if (defaultValue == null) {
            if (!(isEmptyEnable == 1 || isEmptyEnable == null)) {//不为空
                isValidate = false;
            }
        } else {
            tagEntry.val(defaultValue);
        }

    }
    else {
        if (customRegex == null)//执行默认验证
        {
            if (checkType == "string") {
                if (tagValue.length > maxLength || tagValue.length < minLength) {
                    isValidate = false;
                }
            }
            else if (checkType == "int" || checkType == "int32" || checkType == "int16" || checkType == "int64") {
                tagValue = tagValue.replace(/^(\s*)(.*?)(\s*)$/gim, "$2");
                if (tagValue != "-") {
                    if (tagValue.match(regexEnum.intege)) {
                        var intValue = parseInt(tagValue);
                        if (isNaN(intValue)) {
                            isValidate = false;
                        }
                        else {
                            if (intValue > maxValue || intValue < minValue) {
                                isValidate = false;
                            }
                        }
                    } else {
                        isValidate = false;
                    }
                }
            }
            else if (checkType == "decimal" || checkType == "float" || checkType == "double") {
                tagValue = tagValue.replace(/^(\s*)(.*?)(\s*)$/gim, "$2");
                if (tagValue != "-") {
                    var intValue = parseFloat(tagValue);
                    if (isNaN(intValue)) {
                        isValidate = false;
                    }
                    else {
                        if (intValue > maxValue || intValue < minValue) {
                            isValidate = false;
                        }
                    }
                }
            }

            else if (checkType == "datetime") {
                tagValue = tagValue.replace(/^(\s*)(.*?)(\s*)$/gim, "$2");
                if (tagValue != "-") {
                    tagValue = tagValue.replace(/-/g, "/");
                    maxValue = maxValue.replace(/-/g, "/");
                    minValue = minValue.replace(/-/g, "/");
                    var intValue = Date.parse(tagValue);
                    if (isNaN(intValue)) {
                        isValidate = false;
                    }
                    else {
                        if (new Date(intValue) > new Date(Date.parse(maxValue)) || intValue < new Date(Date.parse(minValue))) {
                            isValidate = false;
                        }
                    }
                }
            }

        }
        else {//验证表达式
            if (isRegexEnum == 1) {
                customRegex = eval("regexEnum." + customRegex);
            }
            isValidate = (new RegExp(customRegex).test(tagValue));

        }


    }

    if (isValidate) {
        if (customMethod != null)//执行自定义方法
        {
            var customResult = eval(customMethod + "('" + tagID + "','" + nameSpace + "')");
            if (customResult != "true") {
                isValidate = false;
                msgEntry.attr("customMethodMsg", customResult);
            }
            else {
                msgEntry.attr("customMethodMsg", "");
            }
        }
    }


    if (isValidate) {
        msgEntry.attr("isError", "false");
    }
    else {
        if (validatorSucess && isCheckAll) {
            tagEntry.focus();
        }
        validatorSucess = false;
        msgEntry.attr("isError", "true");
    }
    setHidden(msgID, tagID);


}
function getServerID(tagID) {
    if (!(tagID == null || tagID == "")) {
        var splitStrs = tagID.split("_");
        if (splitStrs.length > 0) {
            return splitStrs[splitStrs.length - 1];
        } else {
            return tagID;
        }

    }
}
//验证是否唯一
function IsUnique(tagID, nameSpace) {
    var tagEntry = $("#" + tagID);
    var txt = getServerID(tagID);
    txt = txt.substring(3);


    var result;
    if (tagEntry.length == 0) {
        return "true";
    } else {
        var value = tagEntry.val();
        if (tagEntry.attr("defaultValue") == value) {
            return "true";
        }
        else {
            result = eval(nameSpace + ".IsUnique" + txt + "(value)");
        }
    }
    if (result.error == null)
    { return result.value; }
    else
    { return "true" }

}
function getFocus(tagID) {
    $("#" + tagID).focus();
}

